package com.uzhie.jt808.task;

import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.msg.JT808MsgBean;
import com.uzhie.jt808.msg.entity.JT_0800;
import com.uzhie.jt808.util.JT808ProtocolUtil;
import com.uzhie.util.ImageUtil;
import com.uzhie.web.service.listener.MQSendService;
import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by TangFei on 2018/9/1.
 * 0801 多媒体数据上传
 */
public class Task0801 implements Runnable {
    private Logger logger = Logger.getLogger(getClass());

    private JT808MsgBean msgBean;
    private MQSendService mqSendService;
    private long mediaID;
    private int channelNum;

    public Task0801(JT808MsgBean msgBean, MQSendService mqSendService) {
        this.msgBean = msgBean;
        this.mqSendService = mqSendService;
        // 多媒体ID
        this.mediaID = JT808ProtocolUtil.parseDWord2Long(msgBean.getBodyBytes(), 0, 4);
        // 通道号
        this.channelNum = JT808ProtocolUtil.parseByte2Int(msgBean.getBodyBytes(), 7);
    }

    @Override
    public void run() {
        ByteArrayOutputStream array = new ByteArrayOutputStream();
        for (byte b : msgBean.getBodyBytes()) {
            array.write(b);
            if (array.size() == JT_0800.getOrderLength()) break;
        }
        mqSendService.sendMessage(JT808Constant.MSG_ID.DEV_REQ_MEDIA_DATA, msgBean.getHeader().getSimNum(), array.toByteArray());
        // 多媒体类型 0：图像；1：音频；2：视频；
        int mediaType = JT808ProtocolUtil.parseByte2Int(msgBean.getBodyBytes(), 4);
        switch (mediaType) {
            case 0:
                logger.info("开始保存图片");
                writeImage();
                logger.info("保存图片结束");
                break;
            default:
                logger.info("啥也不做！");
                break;
        }
    }

    private void writeImage() {
        // 0x0200 位置基本信息时间
        String timeBCD = JT808ProtocolUtil.parseBCDN2String(msgBean.getBodyBytes(), 27, 6);
        if (validateDate(timeBCD, "yyMMddHHmmss")) {
            // 如果校验时间通过 则包含正确的 0X0200 指令 说明为2013 反之为2011
            writeImage2013(msgBean.getBodyBytes());
        } else {
            writeImage2011(msgBean.getBodyBytes());
        }
    }

    private void writeImage2011(byte[] bytes) {
        logger.info("2011");
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 8, msgBean.getHeader().getBodyLength() - 8 - 1);
        ImageUtil.writeImage(byteBuffer.array(), "D:\\dev_img", msgBean.getHeader().getSimNum(), mediaID + "", channelNum + "");
    }

    private void writeImage2013(byte[] bytes) {
        logger.info("2013");
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 36, msgBean.getHeader().getBodyLength() - 36 - 1);
        ImageUtil.writeImage(byteBuffer.array(), "D:\\dev_img", msgBean.getHeader().getSimNum(), mediaID + "", channelNum + "");
    }

    private boolean validateDate(String dateStr, String formateStr) {
        boolean result = false;
        SimpleDateFormat format = new SimpleDateFormat(formateStr);
        try {
            format.setLenient(false);
            format.parse(dateStr);
            result = true;
        } catch (ParseException | NullPointerException e) {
            result = false;
            logger.error("时间格式错误！" + e);
        }
        return result;
    }
}
